;; The first four lines of this file were added by Dracula.
;; They tell DrScheme that this is a Dracula Modular ACL2 program.
;; Leave these lines unchanged so that DrScheme can properly load this file.
#reader(planet "reader.rkt" ("cce" "dracula.plt") "modular" "lang")
(require "Ivolatility.lisp")
(require "Istockhelper.lisp")

(module Mvolatility

(import Istockhelper)
  
#| -------------------------------------------|#

#| *** functions to compute volatility query ***
|#
;; gets daily volatility of a specific ticker 
(defun daily-volatility (ticker-date-data)
  (let* ((data (cdr ticker-date-data))
         (opening-price (first data))
         (highest-price (second data))
         (lowest-price (third data))
         (closing-price (fourth data))
         (day-avg-price (/ (+ opening-price closing-price) 2)))
    (* (/ (- highest-price lowest-price) day-avg-price) 100)))

;; gets sum of daily volatility of a specific ticker over a range of dates
(defun sum-daily-volatility-over-range-of-dates (accumulator data-over-range-of-dates)
  (if (consp data-over-range-of-dates)
      (sum-daily-volatility-over-range-of-dates (+ accumulator
                                                   (daily-volatility (car data-over-range-of-dates)))
                                                (cdr data-over-range-of-dates))
      accumulator))

;; gets volatility of a specific ticker over a range of dates
; format (Ticker, volatility)
(defun volatility (ticker start-date end-date ticker-list flat-data-avl)
  (let* ((ticker-data (get-ticker-data ticker ticker-list flat-data-avl))
         (data-over-range-of-dates (get-data-over-range-of-dates start-date end-date ticker-data))
         (sum-daily-volatilites (sum-daily-volatility-over-range-of-dates 0 data-over-range-of-dates))
         (num-of-days (len data-over-range-of-dates)))
    (if (equal data-over-range-of-dates nil)
        (list (list ticker) sum-daily-volatilites)
        (list (list ticker) (/ sum-daily-volatilites num-of-days)))))

;; helps to get the highest volatility for the volatility query
(defun volatility-query-helper (current-highest-volatility start-date end-date query-tickers ticker-list flat-data-avl)
  (if (consp query-tickers)
      (volatility-query-helper (max-performance/volatility current-highest-volatility
                                                           (volatility (car query-tickers) start-date end-date ticker-list flat-data-avl))
                               start-date
                               end-date
                               (cdr query-tickers)
                               ticker-list
                               flat-data-avl)
      current-highest-volatility))

;; computes volatility query
(defun volatility-query (start-date end-date query-tickers ticker-list flat-data-avl)
  (volatility-query-helper '("" -100) start-date end-date query-tickers ticker-list flat-data-avl))
#| -------------------------------------------|#

(export Ivolatility))